package cn.com.cc.lucene;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Test {
	
	public static void indexFile(File file, IndexWriter indexWriter){
		try{
			File[] files = file.listFiles();
		     for(int i=0; i<files.length; i++){
		    	 if(files[i].isFile()){
		    		 Document document = new Document();
		    		 System.out.println("File " + files[i].getCanonicalPath() + "正在被索引");
		    		 FileInputStream stream = new FileInputStream(files[i]);
		    		 byte[] buffer = new byte[1024];
		    		 int len = 0;
		    		 String content = "";
		    		 while((len = stream.read(buffer)) != -1){
		    			 content += new String(buffer, 0, len);
		    			 
		    		 }
		    		 stream.read(buffer);
		    		 stream.close();
		    		 document.add(new Field("path", files[i].getPath(), TextField.TYPE_STORED));
		    		 document.add(new Field("content", content, TextField.TYPE_STORED));
		    		 indexWriter.addDocument(document);
		    		 indexWriter.commit();
		    	 }else if(files[i].isDirectory()){
		    		 indexFile(files[i], indexWriter);
		    	 }
		     }
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		try{
			//indexDir is the directory that hosts Lucene's index files 
		     File   indexDir = new File("E:\\luceneIndex"); 
		     Directory dir = FSDirectory.open(indexDir);
		     //dataDir is the directory that hosts the text files that to be indexed 
		     File   dataDir  = new File("E:\\luceneData"); 
		     Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_45); 
		     IndexWriter indexWriter = new IndexWriter(dir,new IndexWriterConfig(Version.LUCENE_45, luceneAnalyzer)); 
		     
		     indexFile(dataDir, indexWriter);
		     
		     System.out.println("索引的数目："+indexWriter.numDocs());
		     indexWriter.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

}
